package com.ds.home.comet;

import com.ds.common.JDSException;
import com.ds.context.JDSActionContext;
import com.ds.context.JDSContext;
import com.ds.engine.JDSSessionHandle;
import com.ds.home.udp.SendAppMsg;
import com.ds.msg.Msg;
import com.ds.msg.SensorMsg;
import com.ds.server.JDSClientService;
import com.ds.server.comet.AbstractCometHandle;

import java.io.IOException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

public class AppCometHandle extends AbstractCometHandle{

	private JDSClientService client;
	
	static	ScheduledExecutorService commandService = Executors.newScheduledThreadPool(15);

	
	  public AppCometHandle(JDSClientService client, JDSSessionHandle sessionHandle, String systemCode)
	    throws JDSException
	  {
	    super(client, sessionHandle, systemCode);
	    this.client = client;
	  }
	  
	  
	  
	  

	  public void receive(String receiveStr) throws JDSException
	  {
	    logger.info("receive user:[" + getConnectInfo().getLoginName() + "] ");
	    logger.info(receiveStr);
	  }

	  public boolean send(String msgString)
	    throws JDSException
	  {
	    logger.info(msgString);
	    return super.send(msgString);
	  }

	  public void connect(JDSContext context) throws JDSException
	    
	  {
		//直接发送重置命令
		 this.send("{\"command\":\"InitGateway\"}");
		 
		 try {
			 
			Thread.sleep(1000);
			if (response!=null){
			response.getWriter().print("sessionId is not null,  place login frist!");
			response.getWriter().flush();
			response.flushBuffer();
			}
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	
			

		   
	  }

	  public void disconnect() throws JDSException
	  {
	   

	    super.disconnect();
	  }





	@Override
	public boolean repeatMsg(Msg msg, JDSSessionHandle handle) throws JDSException {
		SendAppMsg msgRun = new SendAppMsg((SensorMsg) msg, handle, JDSActionContext.getActionContext());
		Executors.newSingleThreadExecutor().execute(msgRun);	
		return true;
	}
}
